﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using FrontRoomCheck.Model;
using FrontRoomCheck.ShowDataForm;
using FrontRoomCheck.ShowDataForm.FireProofDirectionCheck;
using FrontRoomCheck.Utiltiy;
using YArchitech.Controls;
using ModelCheckUtility;
using ModelCheckUtility.ControlLayer;
using ModelCheckUtility.ModelLayer;

namespace FrontRoomCheck.SpecialUse
{
	public class ResutlShow_CmdFrontRoomDoorsFilpCheck : IResutlShow<ICloseCurveAnalays>, IPackerResutlShow<ICloseCurveAnalays>, IBase<ICloseCurveAnalays>
	{
		public void ShowResults(List<ICloseCurveAnalays> input, params object[] otherValues)
		{
			List<Phase> list = new List<Phase>();
			this.m_doc = this.m_commandData.Application.ActiveUIDocument.Document;
			this.UiDoc = this.m_commandData.Application.ActiveUIDocument;
			PhaseArray phases = this.m_doc.Phases;
			for (int i = 0; i < phases.Size; i++)
			{
                list.Add(phases.get_Item(i));
			}
			BaseAreaFinder<BaseAreaPacker, FrontRoomPacker, BaseBoundarySegmentPacker, FrontRoomDoorPacker, BaseHostInRoomPacker> baseAreaFinder = new BaseAreaFinder<BaseAreaPacker, FrontRoomPacker, BaseBoundarySegmentPacker, FrontRoomDoorPacker, BaseHostInRoomPacker>();
			this.m_lstAreaPacker = baseAreaFinder.GetAllAreaPackersInDocument(this.m_doc, StaticRescource.m_SelectLevels, input, list, new List<BuiltInCategory>
			{
				(BuiltInCategory)(-2000120)
			}, false);
			int num = 0;
			FireProofDirectionForm fireProofDirectionForm = null;
			try
			{
				this.m_DoorDic = this.GetLevelsFromDoor(this.m_lstAreaPacker);
				foreach (Dictionary<int, List<FamilyInstance>> dictionary in this.m_DoorDic)
				{
					num += dictionary.Count;
				}
				if (num == 0)
				{
					YJKMessageBox.Show("您所检测的楼层中，防火门朝向全部符合规范要求");
				}
				else
				{
					DoorDirectionViewLayerManger doorDirectionViewLayerManger = new DoorDirectionViewLayerManger();
					doorDirectionViewLayerManger.DoViewLayer(this.m_commandData, StaticRescource.m_hWndRevit, this.UiDoc, this.m_lstAreaPacker);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
				if (fireProofDirectionForm != null)
				{
					fireProofDirectionForm.Close();
				}
			}
		}

		public List<Dictionary<int, List<FamilyInstance>>> GetLevelsFromDoor(List<BaseAreaPacker> m_lstAreaPackers)
		{
			bool flag = false;
			Dictionary<BaseDoorPacker, Room> dictionary = new Dictionary<BaseDoorPacker, Room>();
			Dictionary<BaseDoorPacker, Room> dictionary2 = new Dictionary<BaseDoorPacker, Room>();
			Dictionary<int, List<FamilyInstance>> dictionary3 = new Dictionary<int, List<FamilyInstance>>();
			Dictionary<int, Level> m_Level_index = new Dictionary<int, Level>();
			foreach (BaseAreaPacker tmpAreaPacker in m_lstAreaPackers)
			{
				int num = 0;
				dictionary = FrontRoomCheck.Utiltiy.Utility.GetOutDoorNum(tmpAreaPacker);
				dictionary2 = FrontRoomCheck.Utiltiy.Utility.GetFrontRoomAndStairDoor(tmpAreaPacker);
				foreach (KeyValuePair<BaseDoorPacker, Room> keyValuePair in dictionary)
				{
					bool flag2 = ModelCheckUtility.Utility.IfDoorToRoom(keyValuePair.Key, keyValuePair.Value);
					if (flag2)
					{
						this.lstWrongDoor.Add(keyValuePair.Key);
					}
					num++;
				}
				if (num <= 1)
				{
					flag = true;
				}
				foreach (KeyValuePair<BaseDoorPacker, Room> keyValuePair2 in dictionary2)
				{
					bool flag2 = ModelCheckUtility.Utility.IfDoorToRoom(keyValuePair2.Key, keyValuePair2.Value);
					if (flag != flag2)
					{
						this.lstWrongDoor.Add(keyValuePair2.Key);
					}
				}
			}
			foreach (BaseDoorPacker baseDoorPacker in this.lstWrongDoor)
			{
				Parameter parameter = baseDoorPacker.ThisDoor.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
				if (parameter != null)
				{
					Level level = this.m_doc.GetElement(parameter.AsElementId()) as Level;
					if (!dictionary3.Keys.Contains(level.Id.IntegerValue))
					{
						dictionary3.Add(level.Id.IntegerValue, new List<FamilyInstance>());
						m_Level_index.Add(level.Id.IntegerValue, level);
					}
					dictionary3[level.Id.IntegerValue].Add(baseDoorPacker.ThisDoor);
				}
			}
			dictionary3 = (from k in dictionary3
			orderby m_Level_index[k.Key].Elevation
			select k).ToDictionary((KeyValuePair<int, List<FamilyInstance>> k) => k.Key, (KeyValuePair<int, List<FamilyInstance>> k) => k.Value);
			this.m_DoorDic.Add(dictionary3);
			return this.m_DoorDic;
		}

		private List<Dictionary<int, List<FamilyInstance>>> m_DoorDic = new List<Dictionary<int, List<FamilyInstance>>>();

		private List<BaseDoorPacker> lstWrongDoor = new List<BaseDoorPacker>();

		private List<BaseAreaPacker> m_lstAreaPacker = new List<BaseAreaPacker>();

		private ExternalCommandData m_commandData = StaticRescource.CmdData;

		private UIDocument UiDoc;

		private Document m_doc;
	}
}
